/*******************************************************************************

[Last updated: June 4th, 2024]

This script contains the customized Stata functions (programs) that write Latex
code for all administrative tables.

*******************************************************************************/



* Shell for Balance Table and attrition table ----------------------------------

	* Use in balance tabble: Table head											ACTIVE
	capture: program drop table_start_customized_c
	program table_start_customized_c
	args filename table_size caption c0 first_row_size
		local c c
		forvalues i = 1(1)`table_size'{
			local c `c'c
		}
		
			file open table using `filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`caption'} \small%" _n ///
			"\begin{tabular}{p{`first_row_size'pt}`c'} \toprule " _n ///
			"`c0' " _n
	end
	

	* Use in balance and attrition table: Row by Row							ACTIVE
	capture: program drop bal_row_write									
	program define bal_row_write
	args matrixname caption sd index
		
				* Retrive data from matrix
				local CN   : di %9.0fc 		`matrixname'[1,1]
				local CM   : di %5.3f  		`matrixname'[1,2]
				local CSD  : di %5.3f  		`matrixname'[1,3]
				local TN   : di %9.0fc 		`matrixname'[1,4]
				local TM   : di %5.3f  		`matrixname'[1,5]
				local TSD  : di %5.3f  		`matrixname'[1,6]
				local dif  : di %5.3f 		`matrixname'[1,7]
				local se   : di %5.3f 		`matrixname'[1,8]
				local  N   : di %9.0fc 		`matrixname'[1,9]
				local  p   : di %5.3f 		`matrixname'[1,10]
			
				* Format for index				
				if "`index'" == "index"{
				local CM	: di %5.3f  abs(`matrixname'[1,2])
				}
			
				* Star	
				local star = cond(`p'<0.01,"***", cond(`p'<0.05,"**", ///
							 cond(`p'<0.1,"*", cond(`p'>=0.1, "", ""))))
							 
				* Write
				file write table "\parbox[m]{150pt}{\vspace*{5pt}`caption' \vspace* {5pt}}"  
			
				if "`sd'" != "hidesd" {
				file write table " & \begin{tabular}{c@{}c@{}} `CM'  \\ (`CSD') \end{tabular} "
				file write table " & \begin{tabular}{c@{}c@{}} `TM'  \\ (`TSD') \end{tabular} "
				}
				
				if "`sd'" == "hidesd" {
				file write table " & `CM' & `TM' "
				}
				
				file write table " & \begin{tabular}{c@{}c@{}} `dif' \\ (`se')\textsuperscript{`star'} \end{tabular} "
				file write table " & `N' \\ "
		end

	
* Unit Switch ------------------------------------------------------------------ ACTIVE

	capture: program drop table_start_horizontal_us
	program table_start_horizontal_us
	args filename title capsize ColNum 
	
			* Identify number of columns to make
				gl col 
				gl lab
				gl num
				
				forvalues c = 1(1)`ColNum' {
					
						local nm = 4 + `c'
						local cs = 4 + `ColNum' + `c'
						
						gl col ${col}``cs''
						gl lab ${lab} & \multicolumn{1}{c}{\textbf{``nm''}}
						gl num ${num} & \multicolumn{1}{c}{(`c')}

				}
			
			* Set global capsize for box
			gl capsize `capsize'
			
			* Write Expression
			file open table using `filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`title'} \small% " _n ///
			"\begin{tabular}{p{`capsize'pt}${col}} \toprule " _n ///
			"${lab} \\ ${num} \\" _n
			*/
			end

	capture: program drop write_horizontal_unit_switch
	program write_horizontal_unit_switch
	args mat desc version component
	
			* Get the matrix
			mat temp = `mat'
	
			* Get and format estimates
			
				* CM
				local cm :  di %5.3f temp[1,1]
				
				
					if abs(`cm') > 100 {
				    local cm :  di %3.0fc temp[1,1]
					}
					
					*** Addition ***
					if !inlist("`version'", "CmCoSeQvN", "CmCoSeRqN", "CmCoSeN") {
					    local cm \multicolumn{1}{c}{-}
					}
			
				* ITT, SE, N
				local ITT :  di %5.3f temp[1,2]
				local SE  :  di %5.3f temp[1,3]
				local N   :  di %9.0fc temp[1,6]
				
				* P-values
				local p   :  di %5.3f temp[1,4]
				local star = ///
					cond(`p'<0.01,"***", cond(`p'<0.05,"**", ///
					cond(`p'<0.1,"*", cond(`p'>=0.1, "", ""))))
				
				* Q-values
				local q_`var' : di %5.3f temp[1,5]
				local staq = ///
					cond(`q_`var''<0.01,"***", cond(`q_`var''<0.05,"**", ///
					cond(`q_`var''<0.1,"*", cond(`q_`var''>=0.1, "", ""))))
			
			* Write a Tex expression
			
			if "`version'" == "CmCoSeN" {
			file write table "\parbox[m]{${capsize}pt}{\vspace{5pt} `desc' \raggedright \vspace{5pt}} &"
			file write table "`cm' & `ITT' & (`SE')\textsuperscript{`star'} "
			file write table " & `N' \\" _n
			}
			

	end

* Table [midrule] --------------------------------------------------------------

	capture: program drop midrule
	program define midrule
	file write table "\midrule" _n
	end
		


* Table [START] ----------------------------------------------------------------

* [CORE]

	capture: program drop table_start_horizontal
	program table_start_horizontal
	args filename title capsize one_period
	
	* This program is comprehensive, it can write heading for several type of tables
	
		* Multiple period
		gl col 	p{1.8cm}rp{0.15cm}rp{1.3cm}p{1.3cm}p{0.15cm}l
		gl lab 	& \multicolumn{1}{c}{\textbf{Period}} ///
				& \multicolumn{1}{c}{\textbf{CM}} ///
				& & \multicolumn{1}{c}{\textbf{Sit-D}} ///
				& \multicolumn{1}{c}{\textbf{SE}} ///
				& \multicolumn{1}{c}{\textbf{q-value}} ///
				& & \multicolumn{1}{c}{\textbf{N}}
		gl num 	& \multicolumn{1}{c}{(1)} ///
				& \multicolumn{1}{c}{(2)} ///
				& & \multicolumn{1}{c}{(3)} ///
				& \multicolumn{1}{c}{(4)} ///
				& \multicolumn{1}{c}{(5)} ///
				& & \multicolumn{1}{c}{(6)}
				 	
		* One period
		if "`one_period'" == "one"{
		gl col 	rrp{0.15cm}rp{1.3cm}p{1cm}
		gl lab 	& & \multicolumn{1}{c}{\textbf{CM}} ///
				& & \multicolumn{1}{c}{\textbf{Sit-D}} ///
				& \multicolumn{1}{c}{\textbf{SE}} ///
				& \multicolumn{1}{c}{\textbf{q-value}} 
		gl num 	& & \multicolumn{1}{c}{(1)} ///
				& & \multicolumn{1}{c}{(2)} ///
				& \multicolumn{1}{c}{(3)} ///
				& \multicolumn{1}{c}{(4)}
		}

		* One period - with p-value column
		if "`one_period'" == "one_p"{
		gl col 	rrrrrp{0.5cm}p{0.5cm}
		gl lab 	& & \multicolumn{1}{c}{\textbf{CM}} ///
				& & \multicolumn{1}{c}{\textbf{Sit-D}} ///
				& \multicolumn{1}{c}{\textbf{SE}} ///
				& \multicolumn{1}{c}{\textbf{p-value}} ///
				& \multicolumn{1}{c}{\textbf{q-value}} 
		gl num 	& & \multicolumn{1}{c}{(1)} ///
				& & \multicolumn{1}{c}{(2)} ///
				& \multicolumn{1}{c}{(3)} ///
				& \multicolumn{1}{c}{(4)} ///
				& \multicolumn{1}{c}{(5)}
		}
		
		
		* One period - noq
		if "`one_period'" == "one_p_no_q"{
		gl col 	rrrrrp{0.5cm}
		gl lab 	& & \multicolumn{1}{c}{\textbf{CM}} ///
				& & \multicolumn{1}{c}{\textbf{Sit-D}} ///
				& \multicolumn{1}{c}{\textbf{SE}} ///
				& \multicolumn{1}{c}{\textbf{p-value}} 
		gl num 	& & \multicolumn{1}{c}{(1)} ///
				& & \multicolumn{1}{c}{(2)} ///
				& \multicolumn{1}{c}{(3)} ///
				& \multicolumn{1}{c}{(4)}
		}
		
		* NoCM no Q, showing N
		if "`one_period'" == "no_cm_with_n"{
		gl col 	rrrrp{0.5cm}
		gl lab 	& & \multicolumn{1}{c}{\textbf{Coef.}} ///
				& \multicolumn{1}{c}{\textbf{SE}} ///
				& \multicolumn{1}{c}{\textbf{p-value}} ///
				& \multicolumn{1}{c}{\textbf{N}} 
		gl num 	& & \multicolumn{1}{c}{(1)} ///
				& \multicolumn{1}{c}{(2)} ///
				& \multicolumn{1}{c}{(3)} ///
				& \multicolumn{1}{c}{(4)}
		}
		
		* Set global capsize for box
		gl capsize `capsize'
			
		* Write Expression
		file open table using `filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`title'} \small% " _n ///
			"\begin{tabular}{p{`capsize'pt}${col}} \toprule " _n ///
			"${lab} \\ ${num} \\" _n
	end


* [HET - multiple panels]

	capture: program drop table_start_horizontal_het_panel
	program table_start_horizontal_het_panel
	args filename title capsize labhet cus
			
			* Set global capsize for box
			gl capsize `capsize'
			
			* Write Expression
			file open table using `filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`title'} \small%" _n ///
			"\begin{tabular}{p{110pt}rrrrp{1.3cm}p{0.10cm}rrp{1cm}} " _n ///
			"\toprule" _n 
			
	end

* [HET - standard, no CM]

	capture: program drop table_start_horizontal_het_one
	program table_start_horizontal_het_one
	args filename title capsize labhet cus
			
			* Set global capsize for box
			gl capsize `capsize'
			
			* Write Expression
			file open table using `filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`title'} \small%" _n ///
			"\begin{tabular}{p{110pt}rrrrp{1.3cm}p{0.10cm}rrp{1cm}} " _n ///
			"\toprule" _n ///
			" &  & & \multicolumn{3}{c}{\textbf{Sit-D}} &  & \multicolumn{3}{c}{\begin{tabular}[c]{c@{}c@{}} \textbf{Sit-D $ \times $} \\ \textbf{`labhet'} \end{tabular} } \\  " _n ///
			"\hhline{ ~~~===~===}" _n ///
			"\\[-1em]" _n ///
			" & & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} \\ &   & &  \multicolumn{1}{c}{(1)} &  \multicolumn{1}{c}{(2)} &  \multicolumn{1}{c}{(3)} & &  \multicolumn{1}{c}{(4)} &  \multicolumn{1}{c}{(5)} &  \multicolumn{1}{c}{(6)} \\ " _n 
			
	end

* [HET - standard, with CM]

	capture: program drop table_start_horizontal_het_cm
	program table_start_horizontal_het_cm
	args filename title capsize labhet cus
			
			* Set global capsize for box
			gl capsize `capsize'
			
			* Write Expression
			file open table using `filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`title'} \small%" _n ///
			"\begin{tabular}{p{110pt}rrrrp{1.3cm}p{0.10cm}rrp{1cm}} " _n ///
			"\toprule" _n ///
			" & & & \multicolumn{3}{c}{\textbf{Sit-D}} &  & \multicolumn{3}{c}{\begin{tabular}[c]{c@{}c@{}} \textbf{Sit-D $ \times $} \\ \textbf{`labhet'} \end{tabular} } \\  " _n ///
			"\hhline{ ~~~===~===}" _n ///
			"\\[-1em]" _n ///
			" & \multicolumn{1}{c}{CM} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} \\ &  \multicolumn{1}{c}{(1)} & &  \multicolumn{1}{c}{(2)} &  \multicolumn{1}{c}{(3)} &  \multicolumn{1}{c}{(4)} & &  \multicolumn{1}{c}{(5)} &  \multicolumn{1}{c}{(6)} &  \multicolumn{1}{c}{(7)} \\ " _n 
		
	end
			
			
			
			
			
			
			
			
			
			
	
* Table [END] ------------------------------------------------------------------

	* Table ending and reference
	capture: program drop table_end
		program define table_end 
			args note ref
			file write table "\bottomrule \end{tabular}  \begin{tablenotes} \footnotesize \item  `note'" _n
			file write table "\end{tablenotes} \label{`ref'} \end{threeparttable} \end{center}"
			file close table
	end
	
	
	
	
	
	
	
	
	
	
	
	
* Table [IN-LINE] --------------------------------------------------------------

* [CORE-In-line]
	
	capture: program drop write_horizontal
	program write_horizontal
	args mat mul desc version 
		
		* This program is comprehensive, it writes for several type of table format
		
				* Looping through each month segment
				di "Writing result from `mat'"
				gl write_M 
				foreach m in $month_segments {
				di " Writing for `m'"
				
				* Get the matrix
				mat temp = `mat'_`m'
				mat li temp
				* Get and format estimates
				
					* CM
					local cm :  di %5.3f `mul' * temp[1,1]
					
						/*
						if abs(`cm') > 100 {
						local cm :  di %3.0fc temp[1,1]
						}	
						*/
						
					* ITT, SE, N
					local ITT :  di %5.3f 	`mul' * temp[1,2]
					local SE  :  di %5.3f 	`mul' * temp[1,3]
					local N   :  di %9.0fc 		    temp[1,6]
					
					* P-values
					local p   :  di %5.3f temp[1,4]

					local star = ///
						cond(`p'<=0.01,"***", cond(`p'<=0.05,"**", ///
						cond(`p'<=0.1,"*", cond(`p'>0.1, "", ""))))
						
					if temp[1,4] < 0.001 {
						local p   :  di %6.4f temp[1,4]
					}
					
					* Q-values
					local q : di %5.3f temp[1,5]
					local staq = ///
						cond(`q'<=0.01,"***", cond(`q'<=0.05,"**", ///
						cond(`q'<=0.1,"*", cond(`q'>0.1, "", ""))))
				
					* Tag for multiple lines
					if inlist("`m'", "m_1_4") {
					local tag "Months 1-4"
					}
					if inlist("`m'", "m_5_8") {
					local tag "Months 5-8"
					}
					if inlist("`m'", "m_9_12") {
					local tag "Months 9-12"
					}
					if inlist("`m'", "m_1_12") {
					local tag "Months 1-12"
					}				
					* Fabricate le single line
					local cmI \multicolumn{1}{c}{-}
					gl `m'_CmCoSePvQv &  & `cm' & & `ITT' & `SE' & `p'\textsuperscript{`star'} & `q'\textsuperscript{`staq'}      \\
					gl `m'_RcCoSePvQv &  & `cmI' & & `ITT' & `SE' & `p'\textsuperscript{`star'} &`q'\textsuperscript{`staq'}      \\
					gl `m'_CmCoSePvRq &  & `cm' & & `ITT' & `SE' & `p'\textsuperscript{`star'}                                    \\
					gl `m'_RcCoSePvRq &  & `cmI' & & `ITT' & `SE' & `p'\textsuperscript{`star'}       								\\
					gl `m'_TCmCoSePvRq & `tag' & `cm' & & `ITT' & `SE' & `p'\textsuperscript{`star'}                               \\
					gl `m'_CoSePvN    &  & `ITT' & `SE' & `p'\textsuperscript{`star'} & `N'                               \\
					
					gl write_M $write_M ${`m'_`version'}
				}
				
				* Fabricate the block segment
				*gl write_block 	"\multirow{`height_section'}{${capsize}pt}{`desc'} \raggedright $write_M "
				gl write_block 	"\parbox[m]{${capsize}pt}{\vspace{5pt}`desc' \raggedright \vspace{5pt}}$write_M "
				file write table "$write_block" _n
	
	end
	


* [HET-Inline-No CM]
* Heterogeneous Treatment Effect table with NO control mean

	capture: program drop write_horizontal_het_one
	program write_horizontal_het_one
	args mat row mul desc 
	
		
			* Get the matrix
			mat temp = `mat'
	
			* Get and format estimates
			
				* N
				local N   :  di %9.0fc 		        temp[1,8]
				
				* ITT, SE
				local ITT_A :  di %5.3f 	`mul' * temp[1,1]
				local SE_A  :  di %5.3f 	`mul' * temp[1,2]
				local ITT_B :  di %5.3f 	`mul' * temp[1,5]
				local SE_B  :  di %5.3f 	`mul' * temp[1,6]				
				
				* P-values
				local p_A   :  di %5.3f temp[1,3]
				local p_B   :  di %5.3f temp[1,7]
				local star_A = ///
					cond(`p_A'<=0.01,"***", cond(`p_A'<=0.05,"**", ///
					cond(`p_A'<=0.1,"*", cond(`p_A'>0.1, "", ""))))
				local star_B = ///
					cond(`p_B'<=0.01,"***", cond(`p_B'<=0.05,"**", ///
					cond(`p_B'<=0.1,"*", cond(`p_B'>0.1, "", ""))))
				
			* Form a wirte block
			gl write_block \parbox[m]{${capsize}pt}{\vspace{5pt}`desc' \raggedright \vspace{5pt}} &	&& `ITT_A' & `SE_A' & `p_A'\textsuperscript{`star_A'} & & `ITT_B' & `SE_B' & `p_B'\textsuperscript{`star_B'} \\ 
			
		* Write
		file write table "$write_block" _n

	end

* [HET-Inline-With CM]
* Heterogeneous Treatment Effect table with Control mean

	capture: program drop write_horizontal_het_one_cm
	program write_horizontal_het_one_cm
	args mat row mul desc i
	
		
			* Get the matrix
			mat temp = `mat'
	
			* Get and format estimates
			
				* N
				local N   :  di %9.0fc 		        temp[1,8]
				
				* CM
					local cm :  di %5.3f `mul' * temp[1,9]
					
						if abs(`cm') > 100 {
						local cm :  di %3.0fc temp[1,9]
						}
					
				* ITT, SE
				local ITT_A :  di %5.3f 	`mul' * temp[1,1]
				local SE_A  :  di %5.3f 	`mul' * temp[1,2]
				local ITT_B :  di %5.3f 	`mul' * temp[1,5]
				local SE_B  :  di %5.3f 	`mul' * temp[1,6]				
				
				* P-values
				local p_A   :  di %5.3f temp[1,3]
				local p_B   :  di %5.3f temp[1,7]
				local star_A = ///
					cond(`p_A'<=0.01,"***", cond(`p_A'<=0.05,"**", ///
					cond(`p_A'<=0.1,"*", cond(`p_A'>0.1, "", ""))))
				local star_B = ///
					cond(`p_B'<=0.01,"***", cond(`p_B'<=0.05,"**", ///
					cond(`p_B'<=0.1,"*", cond(`p_B'>0.1, "", ""))))
				
			* Write - for raw outcomes
			gl write_block \parbox[m]{${capsize}pt}{\vspace{5pt}`desc' \raggedright \vspace{5pt}} & `cm' & & `ITT_A' & `SE_A' & `p_A'\textsuperscript{`star_A'} & & `ITT_B' & `SE_B' & `p_B'\textsuperscript{`star_B'} \\ 
			
			* Write - for index outcomes
			if "`i'" == "index" {
			gl write_block \parbox[m]{${capsize}pt}{\vspace{5pt}`desc' \raggedright \vspace{5pt}} & \multicolumn{1}{c}{-} & & `ITT_A' & `SE_A' & `p_A'\textsuperscript{`star_A'} & & `ITT_B' & `SE_B' & `p_B'\textsuperscript{`star_B'} \\ 	
			}
						
	* Write into latex		
	file write table "$write_block" _n

	end
	

	
